package com.compomics.proteocloud.jobs;

import java.awt.EventQueue;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashMap;
import java.util.Map;

import com.compomics.proteocloud.sqs.MessageManager;
import com.compomics.proteocloud.sqs.MessageObject;
import com.compomics.proteocloud.util.AWSUtils;
import com.xerox.amazonws.sqs2.Message;
import com.xerox.amazonws.sqs2.SQSException;

/**
 * This class aims to listen to the status queue, receive the messages and convert them to message objects. 
 * @author Thilo Muth
 *
 */
public class DataQueueListener {
	
	/**
	 *  Property change support for notifying the gui about new messages.
	 */
	private PropertyChangeSupport pSupport;
	
	/**
	 * Message manager instance.
	 */
	private MessageManager msgManager;
	
	/**
	 * Singleton fo the StatusQueueListener
	 */
	private static DataQueueListener dataQueueListener;
	
	/**
	 * Message Map (maps the message id to message body)
	 */
	private Map<String, String> messageMap;
	
	/**
	 * Constructor of the StatusQueueListener.
	 * @param msgManager
	 */
	private DataQueueListener(MessageManager msgManager) {
		this.msgManager = msgManager;
		pSupport = new PropertyChangeSupport(this);
	}
	
	/**
	 * Retrieve the instance (singleton object) of this class.
	 * @return dataQueueListener DataQueueListener instance
	 */
	public static DataQueueListener getInstance() {
		if (dataQueueListener == null) {
            MessageManager msgManager = null;
            try {
                msgManager = new MessageManager(AWSUtils.getProperties(), "dataQueue");
                dataQueueListener = new DataQueueListener(msgManager);
            } catch (SQSException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
		}
		return dataQueueListener;
	}
	
	/**
	 * Start the listener.
	 */
	public void start() {
		Message message;
		messageMap = new HashMap<String, String>();
		try {
			while(true){
				message = msgManager.receiveMessage(0);

				// Null check
				if (message != null) {
					if (messageMap.get(message.getMessageId()) == null ) {
						final MessageObject msgObject = new MessageObject();
						msgObject.setMessageid(message.getMessageId());
						msgObject.setMessageBody(message.getMessageBody());
						msgObject.setReceipthandle(message.getReceiptHandle());
						messageMap.put(message.getMessageId(), message.getMessageBody());
						EventQueue.invokeLater(new Runnable() {							
							public void run() {
								pSupport.firePropertyChange("New Message", null, msgObject);							
							}
						});
					}
				}
			}
		} catch (SQSException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Adds the property change listener.
	 * @param l
	 */
	public void addPropertyChangeListener(PropertyChangeListener l ) { 
    	pSupport.addPropertyChangeListener(l); 
	} 
}

